package com.jane.gzzb;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jane.adapter.hostheadlinesadapter.PlatformDetailAdapter;
import com.jane.bean.JsonData;
import com.jane.bean.PlatformDetailBean;
import com.jane.config.NetConfig;
import com.jane.utils.Utils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;

public class PlatformDetailActivity extends AppCompatActivity {
    private String loadUrl;
    private Button mButton;
    private TextView mTextView;
    private GridView mGridView;
    private ImageView mImageView;
    private PlatformDetailAdapter mAdapter;
    private ArrayList<PlatformDetailBean.CommentatorsBean.GameinfoBean> mData;
    private String path;
    private int mPagerIndex;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_platform_detail);
        initView();
        initData();
        setListener();
    }

    private void setListener() {
        mImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }


    private void initData() {
        Intent intent=getIntent();
        loadUrl=intent.getStringExtra("loadUrl");
        mButton.setText(intent.getStringExtra("btnTitle"));
        mTextView.setText(intent.getStringExtra("title"));

        mData=new ArrayList<>();
        mAdapter=new PlatformDetailAdapter(mData,this);
        mGridView.setAdapter(mAdapter);
        //网络加载数据
        path = NetConfig.PLATFORMDETAIL_PATH;
        String jsonString= Utils.getJsonString(path);
        if (!TextUtils.isEmpty(jsonString)){
            parseData(jsonString);
        }else {
            if (!Utils.isNetwork(this)){
                return;
            }else {
                netLoadData();
            }
        }
    }

    private void netLoadData() {
        x.http().get(new RequestParams(path), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if (mPagerIndex==0){
                    JsonData jsonData=new JsonData();
                    jsonData.setJsonString(result);
                    jsonData.setPath(path);
                    Utils.saveJsonString(jsonData);
                }
                if (!TextUtils.isEmpty(result)){
                    parseData(result);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void parseData(String jsonString) {
//        mData.clear();
        try {
            JSONObject object=new JSONObject(jsonString);
            JSONObject commentators = object.getJSONObject("commentators");
            JSONArray gameinfo = commentators.getJSONArray("gameinfo");
            for (int i = 0; i <gameinfo.length(); i++) {
                JSONObject jsonObject = gameinfo.getJSONObject(i);
                PlatformDetailBean.CommentatorsBean.GameinfoBean gameInfo=new PlatformDetailBean.CommentatorsBean.GameinfoBean();
                String rawcoverimage=jsonObject.optString("rawcoverimage");
                String viewers=jsonObject.optString("viewers");
                String name=jsonObject.optString("name");
                String title=jsonObject.optString("title");
                String sourcename=jsonObject.optString("sourcename");
                String commentator=jsonObject.optString("commentator");
                String url=jsonObject.optString("url");
                String roomId=jsonObject.optString("roomid");
                gameInfo.setName(name);
                gameInfo.setRawcoverimage(rawcoverimage);
                gameInfo.setViewers(Integer.parseInt(viewers));
                gameInfo.setTitle(title);
                gameInfo.setSourcename(sourcename);
                gameInfo.setCommentator(commentator);
                gameInfo.setUrl(url);
                gameInfo.setRoomid(roomId);
                mData.add(gameInfo);
            }
//                    Log.e("list",list.toString());
            //TODO 适配器刷新
            mAdapter.notifyDataSetChanged();
        } catch (JSONException e) {
            e.printStackTrace();
        }

       /* PlatformDetailBean bean=new Gson().fromJson(jsonString,PlatformDetailBean.class);
        mData.addAll(bean.getCommentators().getGameinfo());*/
        mAdapter.notifyDataSetChanged();
    }

    private void initView() {
        mButton= (Button) findViewById(R.id.btn_load);
        mTextView= (TextView) findViewById(R.id.tv_title);
        mImageView= (ImageView) findViewById(R.id.iv_back);

        mGridView= (GridView) findViewById(R.id.gridview_platform);

    }

    public void onClick(View view) {
        if (view.getId()==R.id.btn_load){
            Intent intent=new Intent();
            intent.setAction("android.intent.action.VIEW");
            Uri uri=Uri.parse(loadUrl);
            intent.setData(uri);
            startActivity(intent);
        }
    }
}
